package example.二分查找;

// NC105 二分查找-II
public class Demo1 {

    public static int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int minIdx = -1;
        while (left < right) {
            if (nums[left] < target) {
                left++;
            }
            if (nums[left] == target) {
                return left;
            }
            if (nums[right] > target) {
                right--;
            }
            if (nums[right] == target) {
                minIdx = right;
            }
        }
        return minIdx;
    }

    public static void main(String[] args) {
        int search = search(new int[]{1, 2, 4, 4, 5}, 4);
        System.out.println(search);
    }
}
